package xinxin.com.CompositePatternBasic.Component.Composite;

import xinxin.com.CompositePatternBasic.Component.Component;

import java.util.ArrayList;
import java.util.List;

public class Composite extends Component {
    private List<Component> chidlren = new ArrayList<Component>();

    public Composite(String name) {
        super(name);
    }

    @Override
    public void Add(Component component) {
        chidlren.add(component);
    }

    @Override
    public void Remove(Component component) {
        chidlren.remove(component);
    }

    @Override
    public void Display(int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print(' ');
        }
        System.out.println('-' + name);

        for (Component component : chidlren) {
            component.Display(depth + 2);
        }
    }
}
